package com.cc.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.cc.entity.PageRest;
import com.cc.entity.employee;
import com.cc.service.employeeService;
import com.cc.service.impl.employeeServiceImpl;
import com.cc.util.DateUtil;
import com.cc.util.FileUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@WebServlet("/emp.do")
@MultipartConfig
public class employeeServlet extends HttpServlet {
    private employeeService employeeService = new employeeServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println(req.getParameter("method"));
        String method = req.getParameter("method") == null ? "list" : req.getParameter("method");
        switch (method) {
            case "list":
                list(req, resp);
                break;
            case "add":
                add(req, resp);
                break;
            case "edit":
                edit(req, resp);
                break;
            case "update":
                Update(req,resp);
                break;
            case "delete":
                Delete(req,resp);
                break;
            case "page":
                pageList(req,resp);
                break;
        }
    }

    private void pageList(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        // 分页查询
        int pageNum = req.getParameter("pageNum") == null ? 1 : Integer.parseInt(req.getParameter("pageNum"));
        int pageSize = req.getParameter("pageSize") == null ? 5 : Integer.parseInt(req.getParameter("pageSize"));
        // 员工姓名
        String empName = req.getParameter("empName");
        //ctrl+alt+l 格式化代码
        PageRest<employee> pageRest = employeeService.getPage(pageNum, pageSize, empName);
        String json = JSON.toJSONString(pageRest, SerializerFeature.WriteNullListAsEmpty);
        resp.getWriter().write(json);
    }

    private void Delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int empId=Integer.parseInt(req.getParameter("empId"));
        boolean result=employeeService.delete(empId);
        Map<String,Object> map=new HashMap<>();
        if(result){
            map.put("status","success");
            map.put("message","删除成功");
        }else {
            map.put("status","fail");
            map.put("message","删除失败");
        }
        resp.getWriter().write(JSON.toJSONString(map));
    }

    private void Update(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        String id = req.getParameter("id");
        String empname = req.getParameter("empname");
        String password = req.getParameter("password");
        String depId = req.getParameter("depld");
        String creatTime = req.getParameter("createTime");
        String brith = req.getParameter("birthday");
        //获取头像
        Part part=req.getPart("image");
        String path= FileUtil.uploadFile(req,part);
        employee emq = employeeService.getById(Integer.parseInt(id));
        emq.setEmpname(empname);
        emq.setId(Integer.parseInt(id));
        emq.setDepld(String.valueOf(Integer.parseInt(depId)));
        emq.setCreateTime(DateUtil.dateFormat(creatTime));
        emq.setBirthday(DateUtil.dateFormat(brith));
        emq.setImage(path);
        //emq.setPassword(password);
        //emq.setStart(String.valueOf(0));
        boolean a =  employeeService.update(emq);
        if(a){
            resp.sendRedirect("emp.jsp");
        }

    }

    protected void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       String empId=req.getParameter("id");
       employee employee = employeeService.getById(Integer.parseInt(empId));
        req.setAttribute("employee",employee);
        req.getRequestDispatcher("updatteEmp.jsp").forward(req,resp);


    }
    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String empName = req.getParameter("empName");
        List<employee> employeeList = employeeService.getAll(empName);
        String json = JSON.toJSONString(employeeList, SerializerFeature.WriteNullListAsEmpty);
        resp.getWriter().write(json);
    }
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       //获取参数
        String empName = req.getParameter("empname");
        String depld = req.getParameter("depld");
        String starDate = req.getParameter("startTime");
        String phone=req.getParameter("phone");
        String birthday = req.getParameter("birthday");
        //获取part对象
        Part part=req.getPart("image");
        String path= FileUtil.uploadFile(req,part);
        employee eply=new employee();
        eply.setDepld(depld);
        eply.setEmpname(empName);
        eply.setCreateTime(DateUtil.dateFormat(starDate));
        eply.setImage(path);
        eply.setBirthday(DateUtil.dateFormat(birthday));
        //设置员工状态
        eply.setStart("0");
        boolean result=employeeService.add(eply);
        if(result){
            resp.sendRedirect("emp.jsp");
        }
    }
}
